<?php
if(isset($_POST['submit']) && $_POST['submit']=='Masuk') {
	require_once "konfigurasi.php";

	fungsi('login');

	$user	= $_POST['username'];
//	$pass	= md5(strtolower($_POST['password']));
	$pass	= $_POST['password'];

	koneksi_buka();
	$login	= mysql_query("SELECT * FROM guru WHERE nip='$user' AND nip='$pass'");
	$found	= mysql_num_rows($login);
	
	$r		= mysql_fetch_array($login);
	
	if ($found > 0) {

		session_start();
		$_SESSION['id_pengguna']	= $r['id'];
		$_SESSION['jns_pengguna']	= 'guru';
		$_SESSION['nm_pengguna']	= $r['nama'];
		$_SESSION['guru']			= "-";
		$_SESSION['siswa']			= "-";
		$_SESSION['id_guru']		= "-";
		$_SESSION['id_siswa']		= "-";
		$_SESSION['hasil']			= "0";
			$q_guru = mysql_query("SELECT * FROM guru WHERE id=".$r['id']);
			if($dt_guru = mysql_fetch_array($q_guru)){
				$_SESSION['guru'] = $dt_guru['nama'];	
				$_SESSION['nm_pengguna'] = $dt_guru['nama'];
				$_SESSION['id_guru'] = $r['id'];
		
			}
			
		
			$_SESSION['learning']		= true;

			koneksi_tutup();
			loncat("index.php");
	}else {
		
		$login	= mysql_query("SELECT * FROM siswa WHERE nis='$user' AND nis='$pass'");
		$found	= mysql_num_rows($login);
		$r		= mysql_fetch_array($login);
	
		if ($found > 0) {

			session_start();
			$_SESSION['id_pengguna']	= $r['id'];
			$_SESSION['jns_pengguna']	= 'siswa';
			$_SESSION['nm_pengguna']	= $r['nama'];
			$_SESSION['guru']			= "-";
			$_SESSION['siswa']			= "-";
			$_SESSION['id_guru']		= "-";
			$_SESSION['id_siswa']		= "-";
			$_SESSION['hasil']			= "0";
			$q_siswa = mysql_query("SELECT * FROM siswa a left join jadwal_praktikum b on a.id = b.siswa  WHERE a.id=".$r['id']);
			
			date_default_timezone_set('Asia/Jakarta');
		
			if($dt_siswa = mysql_fetch_array($q_siswa)){
				$dw = date('w');
				$hourMin = date('H:i');
				
				
				if($dw!=$dt_siswa['hari'] || $hourMin<$dt_siswa['jam_mulai'] || $hourMin>$dt_siswa['jam_selesai']){
					//~ die;
					koneksi_tutup();
					loncat("index.php?m=jam");
					die;
				}else{
					$_SESSION['siswa'] = $dt_siswa['nama'];	
					$_SESSION['nm_pengguna'] = $dt_siswa['nama'];
					$_SESSION['id_siswa'] = $r['id'];
				}
				
			}			
			
			
			$_SESSION['learning']		= true;

			koneksi_tutup();
			loncat("index.php");
		}else {
			
			loncat("index.php?m=salah");
		}
	}
} else {
	header('location:index.php');
}
?>
